package com.cqeec.demo.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqeec.demo.bean.AdminAuth;
import com.cqeec.demo.bean.AdminRole;
import com.cqeec.demo.bean.AdminUser;
import com.cqeec.demo.bean.MenuTree;
import com.cqeec.demo.service.AdminAuthService;
import com.cqeec.demo.service.AdminRoleService;
import com.cqeec.demo.service.AdminUserService;

/**
 * 系统用户控制器类
 * 
 * @Title: AdminUserController
 * @Descripton: 接收系统用户信息并调用业务逻辑进行处理，然后返回信息给视图层
 */
@Controller
@RequestMapping("/system/user")
public class AdminUserController {
	// 菜单
	@Resource(name = "adminAuthService")
	private AdminAuthService adminAuthService;
	// 角色
	@Resource(name = "adminRoleService")
	private AdminRoleService adminRoleService;
	
	// 用户
	@Resource(name = "adminUserService")
	private AdminUserService adminUserService;
	
	@RequestMapping("/userMenuList")
	@ResponseBody
	public List<MenuTree> getUserMenu(HttpServletRequest request) {
		//获取Session对象
		HttpSession session = request.getSession();
		AdminUser adminUser = (AdminUser)session.getAttribute("system_admin_login_info");
		Long userid = -1L;
		if(adminUser != null)
		{
			userid = adminUser.getId();
		}
		//根据当前用户获取当前用户角色
		List<AdminRole> roles = adminRoleService.getUserRole(userid);
		Iterator<AdminRole> it_role = roles.iterator();
		String str_ids = "";
		String str_role_auth = "";
		while(it_role.hasNext())
		{
			AdminRole role = it_role.next();
			str_ids = str_ids + role.getAuth_ids() + ",";
			str_role_auth = str_role_auth +role.getAuth_desc();
		}
		session.setAttribute("user_role_auth", str_role_auth);
		List<String> ids = Arrays.asList(str_ids.split(","));
		List<AdminAuth> list = adminAuthService.getUserMenu(ids);
		List<MenuTree> userMenuList = new ArrayList<MenuTree>();
		Iterator<AdminAuth> it_auth = list.iterator();
		/*根据easyui tree的json数据格式重新封装数据*/
		while(it_auth.hasNext())
		{
			AdminAuth auth = it_auth.next();
			MenuTree mt = new MenuTree();
			mt.setId(auth.getId());
			mt.setIconCls(auth.getIconCls());
			mt.setPid(auth.getPid());
			mt.setText(auth.getAuth_name());
			mt.setIframe(auth.getIframe());
			Map<String,String> m = new HashMap<String,String>();
			m.put("url", auth.getUrl());
			mt.setAttributes(m);
			String state = adminAuthService.hasChildrens(auth.getId()) ? "closed" : "open";
			mt.setState(state);
			userMenuList.add(mt);
		}
		return userMenuList;
	}
	
	@RequestMapping("/userListByPage")
	@ResponseBody
	public Map<String, Object> getUserListByPage(HttpServletRequest request,@RequestParam("username") String username,
			@RequestParam("page") Integer page,@RequestParam("rows") Integer pagesize) {
		return adminUserService.getUserListByPage(username, page, pagesize);
	}
}
